package com.quzhi.sys.controller.orderController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quzhi.common.Result;
import com.quzhi.sys.entity.Blacklist;
import com.quzhi.sys.service.impl.BlacklistServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器 黑名单用户
 * </p>
 *
 * @author Xibing
 * @since 2024-08-12
 */

@Slf4j
@RestController
@RequestMapping("/sys/blacklist")
public class BlacklistController {

    @Autowired
    private BlacklistServiceImpl blacklistService;


    /**
     * 获取黑名单用户
     * @param blacklist
     * @return
     */
    @GetMapping("/list")
    public Result<?> getBlackList(@RequestParam(value = "username", required = false) String username,
                                  @RequestParam(value = "phone", required = false) String phone,
                                  @RequestParam(value = "pageNo") Integer pageNo,
                                  @RequestParam(value = "pageSize") Integer pageSize) {

        LambdaQueryWrapper<Blacklist> blacklistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        blacklistLambdaQueryWrapper.eq(StringUtils.hasLength(username), Blacklist::getUsername, username);
        blacklistLambdaQueryWrapper.eq(StringUtils.hasLength(phone), Blacklist::getPhone, phone);

        Page<Blacklist> infoPage = new Page<>(pageNo, pageSize);

        blacklistService.page(infoPage, blacklistLambdaQueryWrapper);

        Map<String, Object> data = new HashMap<>();

        data.put("rows", infoPage.getRecords());
        data.put("total", infoPage.getTotal());

        return Result.success(data);
    }


    /**
     * 更具Id获取黑名单用户
     * @param blacklist
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getBlackUserById(@PathVariable String id) {

        return Result.success(blacklistService.getById(id));
    }


    /**
     * 添加黑名单用户
     *
     * @return
     */
    @PostMapping
    public Result<?> addBlackUser(@RequestBody Blacklist blacklist) {
        blacklist.setId(UUID.randomUUID().toString());
        blacklist.setCreateTime(LocalDateTime.now());

        blacklistService.save(blacklist);

        return Result.success("添加成功");
    }

    /**
     * 更新黑名单用户
     *
     * @return
     */
    @PutMapping
    public Result<?> updateBlackUser(@RequestBody Blacklist blacklist) {


        blacklistService.updateById(blacklist);

        return Result.success("跟新成功");
    }


    /**
     * 删除黑名单用户
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> deletedBlackUser(@PathVariable String id) {


        blacklistService.removeById(id);

        return Result.success("删除成功");
    }
}
